package controller.alarm;

import java.util.ArrayList;

import layout.alarm.AudioAdapter;
import layout.alarm.CustomItemAudio;
import object.alarm.Audio;
import utility.alarm.SettingAlarm;
import utility.alarm.StringUlti;
import android.app.ListActivity;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

public class MusicListActivity extends ListActivity implements OnClickListener{

	private Cursor musicCursor;
	public static MediaPlayer mMediaPlayer;
	private ArrayList<Audio> listAudio = new ArrayList<Audio>();
	private int itemSelected = -1;
	private Button btnOk;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.media);
		String[] proj = { MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DATA, 
				MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.SIZE };
		musicCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
		musicCursor.moveToFirst();
		while(!musicCursor.isAfterLast()) {
			Audio audio = new Audio();
			audio.setId(musicCursor.getInt(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
			audio.setData(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
			audio.setName(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
			audio.setDuration(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
			audio.setSize(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
			audio.setSelected(false);
			listAudio.add(audio);
			musicCursor.moveToNext();
		}
		musicCursor = managedQuery(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, proj, null, null, null);
		musicCursor.moveToFirst();
		while(!musicCursor.isAfterLast()) {
			Audio audio = new Audio();
			audio.setId(musicCursor.getInt(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID)));
			audio.setData(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
			audio.setName(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME)));
			audio.setDuration(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
			audio.setSize(musicCursor.getString(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
			audio.setSelected(false);
			listAudio.add(audio);
			musicCursor.moveToNext();
		}
		
		musicCursor.close();
		
		String[] allFiles = StringUlti.getInstance().getAllFilesInAsset(this, SettingAlarm.mediaPath);
		for(int i=0; i<allFiles.length; i++){
			Audio audio = new Audio();
			audio.setId(listAudio.size()+i+1);
			audio.setData("assets/"+allFiles[i]);
			audio.setName(allFiles[i]);
			audio.setDuration("0");
			audio.setSize("0");
			audio.setSelected(false);
			listAudio.add(audio);
		}
		AudioAdapter adapter = new AudioAdapter(getApplicationContext(), listAudio);
		setListAdapter(adapter);
		
		btnOk = (Button) findViewById(R.id.btnOk);
		btnOk.setOnClickListener(this);
		
		mMediaPlayer = new MediaPlayer();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		try {
			CustomItemAudio current = (CustomItemAudio)v;
	    	if (!current.getSelected()) {
	    		if(itemSelected!=-1){
	    			CustomItemAudio prev = (CustomItemAudio)l.getChildAt(itemSelected);
	    			prev.setSelected(false);
	    		}
	    		itemSelected = position;
	    		current.setSelected(true);
	    		String filename = listAudio.get(position).getData();
        		if (mMediaPlayer.isPlaying()) {
        			mMediaPlayer.reset();
        		}
        		if(filename.contains("assets")){
        			AssetFileDescriptor afd = this.getAssets().openFd(SettingAlarm.mediaPath+"/"+listAudio.get(position).getName());
        			mMediaPlayer.setDataSource(afd.getFileDescriptor());
        		}else{
        			mMediaPlayer.setDataSource(filename);
        		}
        		mMediaPlayer.prepare();
        		mMediaPlayer.start();
	    	}else return;
		} catch (Exception e) {	
      	  e.printStackTrace();
      	  return;
  		}
	}
	
	@Override
	protected void onDestroy() {
		mMediaPlayer.stop();
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		if(v==btnOk){
			if(itemSelected==-1) return;
			else{
				AddAlarm.fileAudio[0] = listAudio.get(itemSelected).getName();
				AddAlarm.fileAudio[1] = listAudio.get(itemSelected).getData();
				Intent i = new Intent(this.getApplicationContext(), AddAlarm.class);
				this.startActivity(i);
				this.finish();
			}
		}
	}
}
